/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.h5.controller.order;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.domain.po.UserCartPo;
import com.itttq.gshop.domain.po.UserInfoPo;
import com.itttq.gshop.domain.to.ResultTo;
import com.itttq.gshop.generic.cmpt.annotation.Auth;
import com.itttq.gshop.generic.connector.IUserAddressConnector;
import com.itttq.gshop.generic.connector.IUserCartConnector;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.generic.util.Constants;

@Auth
@RestController
@RequestMapping("cart")
public class CartController extends GenericController {
	private static final long serialVersionUID = -8245776286159142L;
	@Autowired
	private IUserCartConnector userCartConnector;
	@Autowired
	private IUserAddressConnector userAddressConnector;
	/**
	 * 购物车结算页面，只需要传入购物车ID集合。此时还未生成订单
	 * 
	 * @param id	购物车ID集合
	 * @param request
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("settlement.html")
	public ModelAndView settlement(Integer[] id, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/order/settlement");
		List<UserCartPo> carts = userCartConnector.getCartsByUserIdAndIds(this.getUserIdFromSession(request), id);
		// 暂存购物车ID集合到session
		this.setSession(request, Constants.SESSION_CART, carts);
		// TODO GXG 判断库存是否充足
		mv.addObject("carts", carts);
		// 获取默认地址
		mv.addObject("addr", userAddressConnector.getDefaultAddr(this.getUserIdFromSession(request)));
		return mv;
	}
	
	/**
	 * 购物车列表页面
	 * 
	 * @param request
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("list.html")
    public ModelAndView getUserCarts(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/order/cart");
		UserCartPo cart = new UserCartPo();
		cart.setUserId(this.getUserIdFromSession(request));
		List<UserCartPo> carts = userCartConnector.getUserCarts(cart).getData();
		mv.addObject("carts", carts);
        return mv;
    }

	/**
	 * 添加一件商品到购物车
	 * 
	 * @param wareId	商品ID
	 * @Author 迷蝶
	 * @return ResultTo
	 */
	@RequestMapping("addNumber/{wareId}.html")
	public ResultTo addNumber(@PathVariable("wareId") int wareId, HttpServletRequest request) {
		UserInfoPo user = this.getUserFromSession(request);
		return userCartConnector.addNumber(user.getId(), user.getUsername(), wareId, 1);
	}

	/**
	 * 
	 * 设置购物车商品数量
	 * 
	 * @param cartId	购物车ID
	 * @param number	数量
	 * @param request
	 * @Author 迷蝶
	 * @return ResultTo
	 */
	@RequestMapping("setNumber/{cartId}.html")
	public ResultTo saveUserCart(@PathVariable("cartId") int cartId, int number, HttpServletRequest request) {
		return userCartConnector.setNumber(this.getUserIdFromSession(request), cartId, number);
	}

	@RequestMapping("del/{id}.html")
	public Integer delUserCart(@PathVariable("id") int id, HttpServletRequest request) {
		// TODO GXG 此处有个漏洞，没有校验是不是删除自己的购物车
		return userCartConnector.delUserCart(id);
	}
}